<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Models\Base\Charge;
use App\Models\Base\Order;
use App\Models\Base\ServerList;
use App\Service\ChannelService;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;

/**
 * 充值订单.
 */
class OrderController extends Controller
{
    /**
     * [index 订单充值列表].
     *
     * @Author   王文凡
     * @DateTime 2018-03-01
     *
     * @version  1.0
     *
     * @return object
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header(trans('game.order'));
            $amount = $this->sumAmount();
            $content->description(trans('admin.list').'（'.trans('game.amount').'：'.$amount->sum().trans('game.yuan').'  | '.trans('game.number').'：'.$amount->count().'）');
            $content->body($this->grid()->render());
        });
    }

    protected function sumAmount()
    {
        $param = request()->all();
        $where[] = ['status', '=', 1];
        if (!empty($param)) {
            if (isset($param['channel_id']) && $param['channel_id']) {
                $where[] = ['channel_id', '=', $param['channel_id']];
            }
            if (isset($param['server_id']) && $param['server_id']) {
                $where[] = ['server_id', '=', $param['server_id']];
            }
            if (isset($param['player_name']) && $param['player_name']) {
                $where[] = ['player_name', 'like', "%{$param['server_id']}%"];
            }
            if (isset($param['created_at']) && $param['created_at']) {
                if ($param['created_at']['start']) {
                    $where[] = ['created_at', '>=', $param['created_at']['start']];
                }
                if ($param['created_at']['end']) {
                    $where[] = ['created_at', '<=', $param['created_at']['end']];
                }
            }
        }

        return Order::where($where)->pluck('amount');
    }

    /**
     * [grid 列表view逻辑].
     *
     * @Author   王文凡
     * @DateTime 2018-03-01
     *
     * @version  1.0
     *
     * @return object
     */
    protected function grid()
    {
        ServerList::$soft = true;
        $grid = Admin::grid(Order::class, function (Grid $grid) {
            //筛选游戏区服
            if ($server_id = request()->get('server_id')) {
                $grid->model()->where(['server_id' => $server_id]);
            }
            $grid->model()->where('status', 1)->orderBy('id', 'desc');
            $grid->order_sn(trans('game.order_no'));
            $grid->player_id(trans('game.role_id'));
            $grid->player_name(trans('game.role_name'));
            $grid->server()->name(trans('game.server_name'));
            $grid->channel()->name(trans('game.channel'));
            $grid->amount(trans('game.amount'))->sortable();
            $grid->status(trans('game.status'))->display(function ($status) {
                return 1 == $status ? trans('game.is_pay') : trans('game.is_not_pay');
            });
            $grid->platform_order_sn(trans('game.platform_order_no'));
            $grid->order_time(trans('game.pay_time'))->display(function ($time) {
                return date('Y-m-d H:i:s', $time);
            })->sortable();
            $grid->disableActions();
            $grid->disableCreateButton();
            $grid->disableRowSelector();

            // 字段过滤器
            $grid->filter(function ($filter) {
                $filter->disableIdFilter();
                $filter->equal('channel_id', trans('game.channel'))->select(ChannelService::getChannelNameNo());
                $filter->equal('server_id', trans('game.server_name'))->select(ServerList::all()->pluck('name', 'server_id'));
                $filter->like('player_name', trans('game.role_name'));
                $filter->like('order_sn', trans('game.order_no'));
                $filter->between('created_at', trans('game.pay_time'))->date();
            });
        });

        return $grid;
    }

    /**
     * [channelList 渠道二级联动].
     *
     * @Author   王文凡
     * @DateTime 2018-04-02
     *
     * @version  1.0
     *
     * @return Model
     */
    public function channelList(Request $request)
    {
        $q = $request->get('q');
        $all = ['id' => '-1', 'name' => trans('game.channel_all')];
        $charge = Charge::where('game.channel_no', $q)->first();
        if (null == $charge) {
            return [['id' => '-1', 'name' => trans('game.channel_all')]];
        }
        $arr = Charge::where('pid', $charge->id)->select('channel_no as id', 'name')->get()->toArray();

        if (empty($arr)) {
            return [$all];
        } else {
            array_unshift($arr, $all);
        }

        return $arr;
    }
}
